#!/usr/bin/python
 
import sys, os
from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtCore import QObject, SIGNAL
from PyQt4.QtGui import QPixmap, QIcon
from PyQt4.QtGui import (QAbstractItemView, QItemSelectionModel, QListView, QStringListModel, QStandardItemModel, QStandardItem, QListWidgetItem)


#Using loadUiType for faster development (no extra compiling)
#Later, instead of using uic.loadUiType, can compile with 
#  pyuic4 gui.ui -o mygui.py
#Import as object (from mygui import Ui_MainWindow)
#In _init_, set self.ui = Ui_MainWindow
 
class Main(QtGui.QMainWindow):
	def __init__(self, parent=None):
		QtGui.QMainWindow.__init__(self)
		ui_class, widget_class = uic.loadUiType("gui.ui")
		self.ui = ui_class()
		self.ui.setupUi(self)

		self.ui.label_previewArea.setText("hi")
		self.filelist_model = QStandardItemModel()
		
		#Hide options
		self.ui.frame_browser.hide()
		self.ui.frame_options.hide()
		self.resize(350,350)

		dirpath = os.path.expanduser("~/Pictures/in")
		pics = os.listdir(dirpath)
		for p in pics:
			#print p
			item = QStandardItem(QIcon(os.path.join(dirpath,p)), p)
			self.filelist_model.appendRow(item)
		
		self.ui.filelist_browser.setModel(self.filelist_model)
		self.connect(self.ui.filelist_browser.selectionModel(), SIGNAL("selectionChanged(QItemSelection, QItemSelection)"), self.filelist_browser_selection_changed)


	def on_button_import_released(self):
		print "Import" 
		self.ui.filelist_browser.selectedIndexes()
 
	def on_button_close_released(self):
		print "close"	
	
	
	def on_button_show_options_toggled(self, showOptions):
		self.ui.frame_browser.setVisible(self.ui.button_show_options.isChecked())
		self.ui.frame_options.setVisible(self.ui.button_show_options.isChecked())
		if showOptions:
			print "Show Options"
			self.ui.button_show_options.setText("Hide Options")
			self.setMinimumSize(700,620)
		else:
			print "Hide Options"
			self.setMinimumSize(350,350)
			self.resize(350,350)			
			self.ui.button_show_options.setText("Show Options")

	def on_actionPreferences_triggered(self, checked=None):
		if checked is None: return
		prefWin = PrefWindow()
		prefWin.show()
		prefWin.exec_()

	def filelist_browser_selection_changed(self, selected, deselected):
		#print self.filelist_model.data(selected.indexes()[0])
		modelIndexList = self.ui.filelist_browser.selectedIndexes()
		if len(modelIndexList) > 0:
			for index in modelIndexList:
				print index.row()
		else:
			print "Noting selected"
		#print len(selected), "items selected"
		#print len(deselected), "items deselected"
		print


   #-------------------------------
   #Check Boxes, file choosers, rename text entry box
	def on_check_import_photos_toggled(self, value):
		print "toggled import photos:", value

class PrefWindow(QtGui.QDialog):
	def __init__(self, parent=None):
		QtGui.QMainWindow.__init__(self)
		ui_class, widget_class = uic.loadUiType("prefs.ui")
		self.ui = ui_class()
		self.ui.setupUi(self)
 
if __name__ == "__main__":
	app = QtGui.QApplication(sys.argv)
	myapp = Main()
	myapp.show()
	sys.exit(app.exec_())
